package com.yunkaweilai.android.view.dialog.member;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.dialog.utils.CornerUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.member.MemberInfoListActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.interfaces.StringTwoInterface;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.model.member.MemberRecommendModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import org.greenrobot.eventbus.EventBus;

/**
 * 会员推荐人验证
 * Created by Administrator on 2017/6/27.
 */

public class MemberRecommenderDialog extends BaseDialog<MemberRecommenderDialog> {

    TextView idTvRight;
    private Context context;
    private ImageView idImgClose;
    private EditText idEdtRecommendCard;
    private TextView idTvVerification;
    private TextView idTvName;
    private boolean isTrue = false;
    private MemberRecommendModel memberRecommendModel;
    private StringTwoInterface stringTwoInterface;
    private TextView idTvClean;

    public MemberRecommenderDialog(Context context, StringTwoInterface stringTwoInterface) {
        super(context);
        this.context = context;
        this.stringTwoInterface = stringTwoInterface;

    }

    @Override
    public View onCreateView() {
        widthScale(0);
        //showAnim(new Swing());
        //dismissAnim(this, new ZoomOutExit());
        View inflate = View.inflate(mContext, R.layout.dialog_recommender_member, null);
        idTvRight = (TextView) inflate.findViewById(R.id.id_tv_right);
        idImgClose = ((ImageView) inflate.findViewById(R.id.id_img_close));
        idEdtRecommendCard = ((EditText) inflate.findViewById(R.id.id_edt_recommend_card));
        idTvVerification = ((TextView) inflate.findViewById(R.id.id_tv_verification));
        idTvName = ((TextView) inflate.findViewById(R.id.id_tv_name));
        idTvClean = ((TextView) inflate.findViewById(R.id.id_tv_clean));
        //设置背景颜色，和背景的透明圆弧角度
        inflate.setBackgroundDrawable(
                CornerUtils.cornerDrawable(Color.parseColor("#f8f9fa"), dp2px(10)));

        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isTrue){
                    stringTwoInterface.getString(memberRecommendModel.getData().getCard_no(),memberRecommendModel.getData().getMember_name());
                    dismiss();
                }else{
                    if (!StringUtils.isEmpty(idEdtRecommendCard.getText().toString())){
                        MyToast.showShortToast(mContext,"还未验证推荐人卡号、手机号");
                    }else{
                        MyToast.showShortToast(mContext,"还未输入推荐人卡号、手机号");
                    }
                }
            }
        });
        idTvVerification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtRecommendCard.getText().toString())){
                    initHttp();
                }else{
                    MyToast.showShortToast(mContext,"还未输入推荐人卡号、手机号");
                }
            }
        });

        idTvClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stringTwoInterface.getString("","");
                dismiss();
            }
        });
    }

    private void initHttp(){
        HttpRequest.postUrl(Api.VERIFICATION_RECOMMENDER)
                .addParams("type","recommend_cardno")
                .addParams("value",idEdtRecommendCard.getText().toString() + "")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mContext, json);
                        if (checkCode){
                            isTrue = checkCode;
                            Gson gson = new Gson();
                            memberRecommendModel = gson.fromJson(json, MemberRecommendModel.class);
                            idTvName.setText("推荐人姓名：" + memberRecommendModel.getData().getMember_name());

                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(mContext,"系统错误");
                        dismiss();
                    }
                });
    }

}